package com.czk.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 多任务组合
 *
 * @Author:ChenZhangKun
 * @Date: 2022/2/10 16:10
 */
public class CompletableFutureTest5 {
    private static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品图片信息");
            return "1.jpg";
        }, executor);

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品规格信息");
            return "256g";
        }, executor);

        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品品牌信息");
            return "华为";
        }, executor);

        // 组合
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future1, future2, future3);

        allOf.get();

        System.out.println(future1.get()+future2.get()+future3.get());
    }
}
